home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / GNUSUTIL.ZIP / src / id.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-17  |  8.3 KB  |  384 lines

  1. /* id -- print real and effective UIDs and GIDs
  2.    Copyright (C) 89, 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Arnold Robbins, arnold@audiofax.com.
  19.    Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
  20.  
  21. #include <config.h>
  22. #include <stdio.h>
  23. #include <getopt.h>
  24. #include <sys/types.h>
  25. #ifndef OS2
  26. #include <pwd.h>
  27. #include <grp.h>
  28. #endif
  29. #include <getopt.h>
  30.  
  31. #include "version.h"
  32. #include "system.h"
  33.  
  34. #ifndef OS2
  35. #ifdef _POSIX_VERSION
  36. #include <limits.h>
  37. #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
  38. #undef NGROUPS_MAX
  39. #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
  40. #endif /* !NGROUPS_MAX */
  41. #else /* not _POSIX_VERSION */
  42. struct passwd *getpwuid ();
  43. struct group *getgrgid ();
  44. uid_t getuid ();
  45. gid_t getgid ();
  46. uid_t geteuid ();
  47. gid_t getegid ();
  48. #include <sys/param.h>
  49. #if !defined(NGROUPS_MAX) && defined(NGROUPS)
  50. #define NGROUPS_MAX NGROUPS
  51. #endif /* not NGROUPS_MAX and NGROUPS */
  52. #endif /* not _POSIX_VERSION */
  53. #endif /* not OS2 */
  54.  
  55. char *xmalloc ();
  56. #ifndef OS2
  57. int getugroups ();
  58. #endif
  59. void error ();
  60.  
  61. static void print_user ();
  62. static void print_group ();
  63. static void print_group_list ();
  64. static void print_full_info ();
  65. static void usage ();
  66.  
  67. /* The name this program was run with. */
  68. char *program_name;
  69.  
  70. /* If nonzero, output only the group ID(s). -g */
  71. static int just_group = 0;
  72.  
  73. /* If nonzero, output user/group name instead of ID number. -n */
  74. static int use_name = 0;
  75.  
  76. /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
  77. static int use_real = 0;
  78.  
  79. /* If nonzero, output only the user ID(s). -u */
  80. static int just_user = 0;
  81.  
  82. /* If nonzero, output only the supplementary groups. -G */
  83. static int just_group_list = 0;
  84.  
  85. /* The real and effective IDs of the user to print. */
  86. static uid_t ruid, euid;
  87. static gid_t rgid, egid;
  88.  
  89. /* The number of errors encountered so far. */
  90. static int problems = 0;
  91.  
  92. /* If non-zero, display usage information and exit.  */
  93. static int show_help;
  94.  
  95. /* If non-zero, print the version on standard output and exit.  */
  96. static int show_version;
  97.  
  98. static struct option const longopts[] =
  99. {
  100.   {"group", no_argument, NULL, 'g'},
  101.   {"groups", no_argument, NULL, 'G'},
  102.   {"help", no_argument, &show_help, 1},
  103.   {"name", no_argument, NULL, 'n'},
  104.   {"real", no_argument, NULL, 'r'},
  105.   {"user", no_argument, NULL, 'u'},
  106.   {"version", no_argument, &show_version, 1},
  107.   {NULL, 0, NULL, 0}
  108. };
  109.  
  110. void
  111. main (argc, argv)
  112.      int argc;
  113.      char **argv;
  114. {
  115.   int optc;
  116.  
  117.   program_name = argv[0];
  118.  
  119.   while ((optc = getopt_long (argc, argv, "gnruG?", longopts, (int *) 0))
  120.      != EOF)
  121.     {
  122.       switch (optc)
  123.     {
  124.     case 0:
  125.       break;
  126.     case 'g':
  127.       just_group = 1;
  128.       break;
  129.     case 'n':
  130.       use_name = 1;
  131.       break;
  132.     case 'r':
  133.       use_real = 1;
  134.       break;
  135.     case 'u':
  136.       just_user = 1;
  137.       break;
  138.     case 'G':
  139.       just_group_list = 1;
  140.       break;
  141.     default:
  142.       usage (0);
  143.     }
  144.     }
  145.  
  146.   if (show_version)
  147.     {
  148.       printf ("id - %s\n", version_string);
  149.       exit (0);
  150.     }
  151.  
  152.   if (show_help)
  153.     usage (0);
  154.  
  155.   if (just_user + just_group + just_group_list > 1)
  156.     error (1, 0, "cannot print only user and only group");
  157.  
  158.   if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
  159.     error (1, 0, "cannot print only names or real IDs in default format");
  160.  
  161.   if (argc - optind > 1)
  162.     usage (1);
  163.  
  164.   if (argc - optind == 1)
  165.     {
  166.       struct passwd *pwd = getpwnam (argv[optind]);
  167.       if (pwd == NULL)
  168.     error (1, 0, "%s: No such user", argv[optind]);
  169.       ruid = euid = pwd->pw_uid;
  170.       rgid = egid = pwd->pw_gid;
  171.     }
  172.   else
  173.     {
  174.       euid = geteuid ();
  175.       ruid = getuid ();
  176.       egid = getegid ();
  177.       rgid = getgid ();
  178.     }
  179.  
  180.   if (just_user)
  181.     print_user (use_real ? ruid : euid);
  182.   else if (just_group)
  183.     print_group (use_real ? rgid : egid);
  184.   else if (just_group_list)
  185.     print_group_list (argv[optind]);
  186.   else
  187.     print_full_info (argv[optind]);
  188.   putchar ('\n');
  189.  
  190.   exit (problems != 0);
  191. }
  192.  
  193. /* Print the name or value of user ID UID. */
  194.  
  195. static void
  196. print_user (uid)
  197.      int uid;
  198. {
  199.   struct passwd *pwd = NULL;
  200.  
  201.   if (use_name)
  202.     {
  203.       pwd = getpwuid (uid);
  204.       if (pwd == NULL)
  205.     problems++;
  206.     }
  207.  
  208.   if (pwd == NULL)
  209.     printf ("%u", (unsigned) uid);
  210.   else
  211.     printf ("%s", pwd->pw_name);
  212. }
  213.  
  214. /* Print the name or value of group ID GID. */
  215.  
  216. static void
  217. print_group (gid)
  218.      int gid;
  219. {
  220.   struct group *grp = NULL;
  221.  
  222.   if (use_name)
  223.     {
  224.       grp = getgrgid (gid);
  225.       if (grp == NULL)
  226.     problems++;
  227.     }
  228.  
  229.   if (grp == NULL)
  230.     printf ("%u", (unsigned) gid);
  231.   else
  232.     printf ("%s", grp->gr_name);
  233. }
  234.  
  235. /* Print all of the distinct groups the user is in . */
  236.  
  237. static void
  238. print_group_list (username)
  239.      char *username;
  240. {
  241.   print_group (rgid);
  242.   if (egid != rgid)
  243.     {
  244.       putchar (' ');
  245.       print_group (egid);
  246.     }
  247.  
  248. #if defined(NGROUPS_MAX) && defined(HAVE_GETGROUPS)
  249.   {
  250.     int ngroups;
  251.     GETGROUPS_T *groups;
  252.     register int i;
  253.  
  254.     groups = (GETGROUPS_T *) xmalloc (NGROUPS_MAX * sizeof (GETGROUPS_T));
  255.     if (username == 0)
  256.       ngroups = getgroups (NGROUPS_MAX, groups);
  257.     else
  258.       ngroups = getugroups (NGROUPS_MAX, groups, username);
  259.     if (ngroups < 0)
  260.       {
  261.     error (0, errno, "cannot get supplemental group list");
  262.     problems++;
  263.     free (groups);
  264.     return;
  265.       }
  266.  
  267.     for (i = 0; i < ngroups; i++)
  268.       if (groups[i] != rgid && groups[i] != egid)
  269.     {
  270.       putchar (' ');
  271.       print_group (groups[i]);
  272.     }
  273.     free (groups);
  274.   }
  275. #endif
  276. }
  277.  
  278. /* Print all of the info about the user's user and group IDs. */
  279.  
  280. static void
  281. print_full_info (username)
  282.      char *username;
  283. {
  284.   struct passwd *pwd;
  285.   struct group *grp;
  286.  
  287.   printf ("uid=%u", (unsigned) ruid);
  288.   pwd = getpwuid (ruid);
  289.   if (pwd == NULL)
  290.     problems++;
  291.   else
  292.     printf ("(%s)", pwd->pw_name);
  293.   
  294.   printf (" gid=%u", (unsigned) rgid);
  295.   grp = getgrgid (rgid);
  296.   if (grp == NULL)
  297.     problems++;
  298.   else
  299.     printf ("(%s)", grp->gr_name);
  300.   
  301.   if (euid != ruid)
  302.     {
  303.       printf (" euid=%u", (unsigned) euid);
  304.       pwd = getpwuid (euid);
  305.       if (pwd == NULL)
  306.     problems++;
  307.       else
  308.     printf ("(%s)", pwd->pw_name);
  309.     }
  310.   
  311.   if (egid != rgid)
  312.     {
  313.       printf (" egid=%u", (unsigned) egid);
  314.       grp = getgrgid (egid);
  315.       if (grp == NULL)
  316.     problems++;
  317.       else
  318.     printf ("(%s)", grp->gr_name);
  319.     }
  320.  
  321. #if defined(NGROUPS_MAX) && defined(HAVE_GETGROUPS)
  322.   {
  323.     int ngroups;
  324.     GETGROUPS_T *groups;
  325.     register int i;
  326.  
  327.     groups = (GETGROUPS_T *) xmalloc (NGROUPS_MAX * sizeof (GETGROUPS_T));
  328.     if (username == 0)
  329.       ngroups = getgroups (NGROUPS_MAX, groups);
  330.     else
  331.       ngroups = getugroups (NGROUPS_MAX, groups, username);
  332.     if (ngroups < 0)
  333.       {
  334.     error (0, errno, "cannot get supplemental group list");
  335.     problems++;
  336.     free (groups);
  337.     return;
  338.       }
  339.  
  340.     if (ngroups > 0)
  341.       fputs (" groups=", stdout);
  342.     for (i = 0; i < ngroups; i++)
  343.       {
  344.     if (i > 0)
  345.       putchar (',');
  346.     printf ("%u", (unsigned) groups[i]);
  347.     grp = getgrgid (groups[i]);
  348.     if (grp == NULL)
  349.       problems++;
  350.     else
  351.       printf ("(%s)", grp->gr_name);
  352.       }
  353.     free (groups);
  354.   }
  355. #endif
  356. }
  357.  
  358. static void
  359. usage (status)
  360.      int status;
  361. {
  362.   if (status != 0)
  363.     fprintf (stderr, "Try `%s --help' for more information.\n",
  364.          program_name);
  365.   else
  366.     {
  367.       print_version("id");
  368.       printf ("Usage: %s [OPTION]... [USERNAME]\n", program_name);
  369.       printf ("\
  370. \n\
  371.   -g, --group     print only the group ID\n\
  372.   -G, --groups    print only the supplementary groups\n\
  373.   -n, --name      print a name instead of a number, for -ugG\n\
  374.   -r, --real      print the real ID instead of effective ID, for -ugG\n\
  375.   -u, --user      print only the user ID\n\
  376.       --help      display this help and exit\n\
  377.       --version   output version information and exit\n\
  378. \n\
  379. Without any OPTION, print some useful set of identified information.\n\
  380. ");
  381.     }
  382.   exit (status);
  383. }
  384.